package com.eve.eplus.main;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eve.eplus.R;
import com.eve.eplus.common.activity.BaseActivity;
import com.eve.eplus.common.adapter.SendMsgAccountListAdapter;
import com.eve.eplus.common.app.BaseApplication;
import com.eve.eplus.common.app.Constants;
import com.eve.eplus.common.tools.ComImgTools;
import com.eve.eplus.common.tools.ConfigurationTools;
import com.eve.eplus.common.view.ComToastTools;
import com.eve.eplus.main.Fragment.ContentFragment;
import com.eve.eplus.service.SendMsgService;
import com.eve.eplus.sns.SNSAccountManager;

/**
 * 发送微博界面
 * 
 * @author Norsie
 * @create 12-11-29
 */
public class SendMsgActivity extends BaseActivity
{

    private Context mContext = SendMsgActivity.this;
    private String mContent; // 微博内容
    private String mPicUri;

    private TextView tvContent;
    private ImageView ivPic, ivSend;
    private ListView lvAccountList;
    private LinearLayout llSendBar;
    private SendMsgAccountListAdapter adapter;
    private ArrayList<HashMap<String, String>> mSelectList = new ArrayList<HashMap<String, String>>();
    private ArrayList<HashMap<String, String>> mSendList = new ArrayList<HashMap<String, String>>();

    // Notification相关变量
    private NotificationManager notificationManager;
    private Notification.Builder mBuilder;
    private Notification mNotification;

    // Service
    private SendMsgService.SendBinder binder;
    private boolean serviceBinded;

    private Activity mShareActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sendmsg_main);
        mContent = getIntent().getStringExtra(Constants.SNS_SEND_CONTENT);
        mPicUri = getIntent().getStringExtra(Constants.SNS_SEND_PICURI);
        initView();
        this.mSelectList = SNSAccountManager.getAccountList();
        adapter = new SendMsgAccountListAdapter(mContext,
                                                BaseApplication.SNS_ACCOUNT);
        lvAccountList.setAdapter(adapter);
        tvContent.setText(mContent);
        if (mPicUri != null)
        {
            ivPic.setVisibility(View.VISIBLE);
            BitmapFactory.Options options = new BitmapFactory.Options(); 
            options.inSampleSize = 2;
            Bitmap bm = BitmapFactory.decodeFile(mPicUri, options); 
            ivPic.setImageBitmap(bm);
        }
        else
            ivPic.setVisibility(View.GONE);

    }

    /**
     * ServiceConnection对象
     */
    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service)
        {
            // 当连接服务的时候此方法被调用
            binder = (SendMsgService.SendBinder) service;
            binder.start(mSendList, mContent, mPicUri); // 开始
            serviceBinded = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name)
        {
            // 当断开服务的时候此方法被调用

        }
    };

    /**
     * 发送微博
     */
    private void send()
    {
        boolean isGoOn = true;
        // 判断是否有选中某个条目
        for (int i = 0; i < mSelectList.size(); i++)
        {
            if (mSelectList.get(i).containsKey("isSelect"))
                if (mSelectList.get(i).get("isSelect").equals("true"))
                    break;
            if (i == mSelectList.size() - 1)
            {
                // 未选中任何条目
                Animation cycleAnim = AnimationUtils.loadAnimation(mContext,
                        R.anim.sendbtn_anim);
                ivSend.startAnimation(cycleAnim);
                isGoOn = false;
                ComToastTools.ShowMsg(mContext, "请至少选择一个目标帐号", "l");
            }

        }
        if (isGoOn)
        {
            for (int i = 0; i < mSelectList.size(); i++)
            {
                if (mSelectList.get(i).containsKey("isSelect"))
                    if (mSelectList.get(i).get("isSelect").equals("true"))
                        mSendList.add(mSelectList.get(i));
            }
            // 启动服务
             Intent intent =new Intent(mContext, SendMsgService.class);
             startService(intent);
             bindService(intent, conn, Context.BIND_AUTO_CREATE);
             //置空草稿内容
             ConfigurationTools.getInstance(mContext).putString(Constants.SAVED_MSG, "");

            if (BaseApplication.mShareObj instanceof Activity)
            {
                mShareActivity = (Activity) BaseApplication.mShareObj;
                mShareActivity.finish();
            }
            BaseApplication.mShareObj = null;
            finish();
        }

    }

    /**
     * 初始化界面
     */
    private void initView()
    {
        tvContent = (TextView) findViewById(R.id.content);
        ivPic = (ImageView) findViewById(R.id.pic);
        lvAccountList = (ListView) findViewById(R.id.listView);
        ivSend = (ImageView) findViewById(R.id.iv_send);
        llSendBar = (LinearLayout) findViewById(R.id.bar);
        lvAccountList.setOnItemClickListener(onItemClickListener);
        llSendBar.setOnClickListener(onClickListener);

    }

    /*
     * 刷新界面
     */
    private void viewReflash()
    {
        mSelectList = SNSAccountManager.getAccountList();
        // 将之前的选中背景颜色恢复
        for (int i = 0; i < mSelectList.size(); i++)
        {
            if (lvAccountList.getChildAt(i) != null)
                lvAccountList.getChildAt(i).findViewById(R.id.id_item_account).setBackgroundColor(
                        Color.TRANSPARENT);
        }

    }


    /**
     * Listview项 点击事件
     */
    OnItemClickListener onItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id)
        {
            ImageView mImgSelect=(ImageView)view.findViewById(R.id.item_check);
            if (!mSelectList.get(position).containsKey("isSelect"))
            {
                mSelectList.get(position).put("isSelect", "true");
                mImgSelect.setBackgroundResource(R.drawable.icon_set_check);
            }
            else
            {
                if (mSelectList.get(position).get("isSelect").equals("true"))
                {
                    mSelectList.get(position).put("isSelect", "false");
                    mImgSelect.setBackgroundResource(R.drawable.icon_set_uncheck);
                }
                else
                {
                    mSelectList.get(position).put("isSelect", "true");
                    mImgSelect.setBackgroundResource(R.drawable.icon_set_check);
                }
            }
        }
    };

    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.bar:
                    send();
                    break;

                default:
                    break;
            }

        }
    };

    public void onBackPressed()
    {
        finish();
    };

    @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (serviceBinded)
            unbindService(conn);
    }
}
